﻿package
{
	public class BuddyListDS
	{
		import BuddyDS;
		
		var BuddyDSlist: Array;
		
		public function BuddyListDS()
		{
			BuddyDSlist = new Array();
		}
		
		public function addBuddyDS(bud: BuddyDS): void
		{
			if (!this.hasBuddyDS(bud.getBuddyID()))
			{
				BuddyDSlist.push(bud);
				BuddyDSlist.sort();
			}
		}
		
		public function removeBuddyDS(BuddyDSname:String): void
		{
			function isSame(theItem:BuddyDS, index:int, arr:Array):Boolean
			{
				return !(theItem.getBuddyID() == BuddyDSname);
			}
			
			BuddyDSlist = BuddyDSlist.filter(isSame);
		}
		
		public function changeBuddyStatus(b:BuddyDS)
		{
			for each (var bud:BuddyDS in BuddyDSlist)
			{
				if (bud.getBuddyID() == b.getBuddyID())
				{
					bud.status = b.status;
				}
			}
		}
		
		public function getBuddyDS(BuddyDSname:String):BuddyDS
		{
			for each (var b:BuddyDS in BuddyDSlist)
			{
				if(b.getBuddyID() == BuddyDSname)
				{
					return b;
				}
			}
			return null;
		}
		
		public function hasBuddyDS(BuddyDSname:String): Boolean
		{
			for each (var b:BuddyDS in BuddyDSlist)
			{
				if(b.getBuddyID() == BuddyDSname)
				{
					return true;
				}
			}
			return false;
		}
		
		public function getList(): Array
		{
			return BuddyDSlist;
		}
		
		public function sortOn(sorter:Function, reverse:Boolean):BuddyListDS
		{
			BuddyDSlist.sort(sorter);
			
			if (reverse)
			{
				BuddyDSlist.reverse();
			}
			
			return this;
		}
	}
}